﻿// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import mybutton from './components/common/myButton'
import router from './router'
import ElementUI from 'element-ui'
import axios from 'axios'
import Vuex from 'vuex'
import echarts from 'echarts'

import store from './vuex/store.js'
import { bus } from './assets/js/bus.js'
import sess from './assets/js/sess.js'
import cookie from './assets/js/cookie.js'
import api from './assets/js/api.js'

import './assets/js/filter.js'
import './assets/js/config.js'

// import 'element-ui/lib/theme-chalk/index.css'
import './assets/theme/index.css'
import './assets/css/base.css'
import './assets/fonts/iconfont/iconfont.css'

Vue.config.productionTip = false;

Vue.prototype.$axios = axios;
Vue.prototype.$sess = sess;
Vue.prototype.$cookie = cookie;
Vue.prototype.$bus = bus;
Vue.prototype.$api = api;
Vue.prototype.$echarts = echarts;

Vue.prototype.$setMarketId = function () {
    Vue.prototype.$marketId = Vue.prototype.$sess.get('userInfo').accountPropertyMap.marketId;//设置全局市场id
    Vue.prototype.$shopId = Vue.prototype.$sess.get('userInfo').accountPropertyMap.shopId || ''; //设置全局市场id
};

Vue.use(ElementUI);
Vue.use(Vuex);

/* eslint-disable no-new */

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    if (!sess.get('loginState')) {
      next({path: '/login'})
    } else {
      let path;
      if(to.params.id){
        path = to.path.replace(`/${to.params.id}`,'');
      }else {
        path = to.path
      }
      if (sess.get('routeArray').indexOf(path) < 0) {
        // sess.remove('loginState');
        // sess.remove('userInfo');
        // next({path: '/login'})
        next()
      } else {
        next()
      }
    }
  }
});

Vue.component('mybutton', mybutton);//注册全局组件，处理重复提交问题

new Vue({
    el: '#app',
    router,
    store,
    components: { App },
    created() {
        if (this.$sess.get('userInfo')) {
            this.$setMarketId();
        }
    },
    template: '<App/>'
});

